package com.le.basic.Sort;

import java.util.Arrays;

/**
 * 冒泡排序, 利用对数器进行测试
 */
public class Code_01_BubbleSort {

    public static void bubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = arr.length - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    // 对数器测试算法

    /**
     * 生成随机数组
     *
     * @param maxSize  数组最大规模
     * @param maxValue 数组最大值
     * @return
     */
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int temp[] = new int[(int) (Math.random() * (maxSize + 1))];
        for (int i = 0; i < temp.length; i++) {
            temp[i] = (int) ((maxValue + 1) * Math.random() - maxSize * Math.random());
        }
        return temp;
    }

    /**
     * 绝对正确的排序方法, 但效率低
     *
     * @param arr
     */
    public static void comparator(int[] arr) {
        if (arr == null) {
            return;
        }
        Arrays.sort(arr);
    }

    /**
     * 数组复制
     *
     * @param arr
     * @return
     */
    public static int[] copyArray(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }
        int[] ints = new int[arr.length];
        for (int i = 0; i < ints.length; i++) {
            ints[i] = arr[i];
        }
        return ints;
    }

    /**
     * 判断两个数组绝对相等
     *
     * @param arr1
     * @param arr2
     * @return
     */
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr2 == null && arr1 != null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }

        if (arr1.length != arr2.length) {
            return false;
        }

        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static void printArray(int[] arr) {
        if (arr == null) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "  ");
        }
        System.out.println();
    }

    // test
    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
            int[] array = generateRandomArray(maxSize, maxValue);
            int[] array1 = copyArray(array);
            int[] array2 = copyArray(array);
            bubbleSort(array1);
            comparator(array2);
            if (!isEqual(array1, array2)) {
                succeed = false;
                break;
            }
        }
        System.out.println(succeed ? "nice" : "ddddddd");
//        int[] temp = generateRandomArray(maxSize,maxValue);
//        bubbleSort(temp);
//        printArray(temp);
    }
}
